package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.domain.Course;
import com.woniu.domain.CourseManagementDTO;
import com.woniu.domain.MapPath;
import com.woniu.domain.User;
import com.woniu.service.CourseService;
import com.woniu.service.MapPathService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: zcl  <br/>
 * Date: 2021/6/8:17:47  <br/>
 * Description:学习路径控制层
 */
@RestController
@RequestMapping("/mapPath")
public class MapPathController {
    @Autowired(required = false)
    MapPathService mapPathService;
    @Autowired(required = false)
    CourseService courseService;

    @Autowired(required = false)
    RedisTemplate redisTemplate;


    /*
     * Description : TODO  <br/>
     * ChangeLog : 1. 创建 (2021/6/8 17:49 [zcl]);
     ** @param null
     * *  @return null
     */
    @ApiOperation(value = "新增路径")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mapPath", value = "路径对象", required = true, paramType = "body", dataType = "MapPath", dataTypeClass = MapPath.class),
            @ApiImplicitParam(name = "httpServletRequest", value = "用户获取请求头信息（登录token）"),
    })
    @PostMapping("/addMapPath")
    public String addMapPath(@RequestBody MapPath mapPath, HttpServletRequest httpServletRequest) {
        String msg = "";
        //从redis中通过token键值取出对应的对象
        User user = (User) redisTemplate.opsForValue().get(httpServletRequest.getHeader("Authorization"));
        mapPath.setCreator(user.getUserName());
        //路径名称查重
        MapPath mp = mapPathService.findMapPathAndByName(mapPath.getPathName());
        if (mp != null) {
            //该名称已存在
            msg = "路径名称已存在";
        } else {
            int res = mapPathService.addMapPathAndMapPathExamConnect(mapPath);
            if (res >= 2) {
                msg = "添加路径成功";
            } else {
                msg = "添加路径失败";
            }
        }
        return msg;
    }

    @ApiOperation(value = "查询所有路径")
    @GetMapping("/findAllMapPath")
    public List<MapPath> findAllMapPath() {
        return mapPathService.findAllMapPath();
    }

    @ApiOperation(value = "查询指定id路径对应的中间表课程数量")
    @ApiImplicitParam(name = "refPathId", value = "路径id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    @GetMapping("/findMapPathById")
    public MapPath findMapPathById(Integer refPathId) {
        return mapPathService.findMapPathById(refPathId);
    }

    @ApiOperation(value = "通过课件的ids批量查询")
    @ApiImplicitParam(name = "courseIds", value = "课程id字符串", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    @GetMapping("/findCoursesByIds")
    public List<Course> findCoursesByIds(String courseIds) {

        String[] cids = courseIds.split(",");
        return courseService.findCoursesByIds(cids);
    }

    @ApiOperation(value = "通过路径id修改")
    @ApiImplicitParam(name = "id", value = "路径id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    @PostMapping("/updateMapPathById")
    public int updateMapPathById(@RequestBody MapPath mapPath) {
        System.out.println(mapPath);
        return mapPathService.updateMapPathById(mapPath);
    }

    @RequestMapping("getMapPathByMapId")
    public List<MapPath> getMapPathByMapId(Integer mapId) {
        return mapPathService.getMapPathByMapId(mapId);
    }

    @RequestMapping("queryAllMapPath")
    public PageInfo<MapPath> queryAllMapPath(Integer page ,Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<MapPath> mapPaths = mapPathService.queryAllMapPath();
        PageInfo<MapPath> pageInfo = new PageInfo<>(mapPaths);
        return pageInfo;
    }

    @RequestMapping("cqueryAllMapPath")
    public PageInfo<MapPath> CqueryAllMapPath(@RequestBody CourseManagementDTO<MapPath> courseManagementDTO){
        PageHelper.startPage(courseManagementDTO.getPage(),courseManagementDTO.getPageSize());
        List<MapPath> mapPaths = mapPathService.cqueryAllMapPath((MapPath)courseManagementDTO.getObj());
        PageInfo<MapPath> pageInfo = new PageInfo<>(mapPaths);
        return pageInfo;
    }

    @RequestMapping("delMapPath")
    public Integer delMapPath(Integer id){
        return mapPathService.delMapPath(id);
    }
}



